	package server;
import java.util.*;

import lagern.Lager;
import lagern.LagerHelper;

import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.*;

/**
 * Server class
 * @author Laurine and Vincent
 *
 */
public class Server {
	public static void main(String args[]) {
		try {
			//args[0] = host
			//args[1] = port
			//to check if the port is an Integer. If not, an NumberFormatException is thrown
			Integer.parseInt(args[1]);
			//Initialization of the Corba bus
			Properties props = new Properties();
			props.put("org.omg.CORBA.ORBInitialPort", args[1]);
			props.put("org.omg.CORBA.ORBInitialHost",args[0]);
			ORB orb = ORB.init(args, props);
			System.out.println("ORB initialized");
			
			//get the reference of rootPOA and activate POA Manager
			POA rootPoa =
					POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootPoa.the_POAManager().activate();
			
			//Creation of the servant
			LagerImpl lager = new LagerImpl(rootPoa);
			rootPoa.activate_object(lager);
			
			//get the object reference of Lager
			org.omg.CORBA.Object ref = rootPoa.servant_to_reference(lager);
			
			Lager lagerRef = LagerHelper.narrow(ref);
			
			// get the root naming context
	        org.omg.CORBA.Object objRef =
	        orb.resolve_initial_references("NameService");
	        //Use NamingContextExt instead of NamingContext. This is
	        // part of the Interoperable naming Service.
	        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			
			//bind the reference of "lagerRef" with the name "lager"
			ncRef.rebind(ncRef.to_name("lager"), lagerRef);
			
			System.out.println("Server is ready and waiting for a request...");
			
			orb.run();
			
			
			
			
		}catch(NumberFormatException e){
			System.err.println("Please check the port");
		}	
		catch(Exception e)
		{
			System.err.println("Problem with the server");
			System.err.println("<Corba Name Service host> <Corba Name Service port>");
		}
	}

}
